home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / sources / outputfile.cp < prev    next >
Text File  |  1995-09-29  |  3KB  |  103 lines

  1. #if defined( macintosh) || defined( __MWERKS__)
  2.     #include <Files.h>
  3.     #include <OSUtils.h>
  4.     #if defined( __SC__)
  5.         #include <pascal.h>
  6.     #endif
  7.     #include <Finder.h>        // for kNameLocked
  8. #else
  9.     #include <time.h>
  10. #endif
  11.  
  12. #include <iostream.h>
  13. #include <fstream.h>
  14. #include <string.h>
  15.  
  16. #include "outputfile.h"
  17. //
  18. // set2chars is a quick hack which puts the ascii representation of the last two
  19. // digits of the number passed in in the two characters pointed to by two_chars.
  20. //
  21. void outputfile::set2chars( const int n, char *two_chars)
  22. {
  23.     const char lastdigit  = n % 10;
  24.     const char firstdigit = ((n - lastdigit) / 10) % 10;
  25.     two_chars[ 0] = firstdigit + '0';
  26.     two_chars[ 1] = lastdigit  + '0';
  27. }
  28.  
  29. #if defined( macintosh) || defined( __MWERKS__)
  30.     void outputfile::setoutfilename( char *yymmdd_hhmmss_plus_postfix)
  31.     {
  32.         DateTimeRec now;    
  33.         GetTime( &now);
  34.         
  35.         set2chars( now.year,    &yymmdd_hhmmss_plus_postfix[  0]);
  36.         set2chars( now.month,   &yymmdd_hhmmss_plus_postfix[  2]);
  37.         set2chars( now.day,     &yymmdd_hhmmss_plus_postfix[  4]);
  38.         set2chars( now.hour,    &yymmdd_hhmmss_plus_postfix[  7]);
  39.         set2chars( now.minute,  &yymmdd_hhmmss_plus_postfix[  9]);
  40.         set2chars( now.second,  &yymmdd_hhmmss_plus_postfix[ 11]);
  41.     }
  42. #else
  43.     void outputfile::setoutfilename( char *yymmdd_hhmmss_plus_postfix)
  44.     {
  45.         const time_t now_in_seconds = time( NULL);
  46.         
  47.         struct tm *now = localtime( &now_in_seconds);
  48.  
  49.         set2chars( now->tm_year, &yymmdd_hhmmss_plus_postfix[  0]);
  50.         set2chars( now->tm_mon,  &yymmdd_hhmmss_plus_postfix[  2]);
  51.         set2chars( now->tm_mday, &yymmdd_hhmmss_plus_postfix[  4]);
  52.         set2chars( now->tm_hour, &yymmdd_hhmmss_plus_postfix[  7]);
  53.         set2chars( now->tm_min,  &yymmdd_hhmmss_plus_postfix[  9]);
  54.         set2chars( now->tm_sec,  &yymmdd_hhmmss_plus_postfix[ 11]);
  55.     }
  56. #endif
  57.  
  58. #ifdef it_doesnt_work
  59.     outputfile::outputfile( char *postfix, Boolean name_locked)
  60. #else
  61.     outputfile::outputfile( char *postfix, Boolean name_locked) : ofstream()
  62. #endif
  63. {
  64.     strcpy( filename, "yymmdd hhmmss ");
  65.     //
  66.     // This is sloppy programming (the 200 limit is to low), but certainly works
  67.     // and 200 bytes for a filename is long enough (certainly on a Mac)
  68.     //
  69.     strncat( filename, postfix, 200);
  70.     setoutfilename( filename);
  71.  
  72.     #if defined( macintosh) || defined( __MWERKS__)
  73.         filename[ 31] = 0;    // 940309: limit length of filename on Mac to 31
  74.         CtoPstr( filename);
  75.         (void)Create( (ConstStr255Param)filename, 0, 'R*ch', 'TEXT');
  76.  
  77.         if( name_locked)
  78.         {
  79.             FInfo theInfo;
  80.             if( GetFInfo( (ConstStr255Param)filename, 0, &theInfo) == noErr)
  81.             {
  82.                 theInfo.fdFlags &= ~kHasBeenInited;
  83.                 theInfo.fdFlags |=  kNameLocked;
  84.                 (void)SetFInfo( (ConstStr255Param)filename, 0, &theInfo);
  85.             }
  86.         }
  87.         PtoCstr( (unsigned char *)filename);
  88.     #endif
  89.     
  90.     #ifndef it_doesnt_work
  91.         #ifdef THINK_CPLUS
  92.             ofstream::open( filename, ios::app | ios::out | ios::translated);
  93.         #else
  94.             ofstream::open( filename, ios::app | ios::out);
  95.         #endif
  96.     #endif
  97. }
  98.  
  99. char *outputfile::operator()() const
  100. {
  101.     return (char *)filename;
  102. }
  103.